package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.CangkuKuCunMapper;
import com.zhongyu.mapper.KuCunBdMapper;
import com.zhongyu.pojo.CangkuKucun;

import com.zhongyu.pojo.KucunBiandong;
import com.zhongyu.pojo.Order;
import com.zhongyu.pojo.OrderDetail;
import com.zhongyu.service.KuCunService;
import com.zhongyu.vo.KuCunVo;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service("KuCunService")
public class KuCunServicelmpl extends ServiceImpl<CangkuKuCunMapper,CangkuKucun> implements KuCunService {
  @Resource
  private   CangkuKuCunMapper cangkuKuCunMapper;
    @Resource
    private KuCunBdMapper kuCunBdMapper;
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, KuCunVo kuCunVo) {
        LambdaQueryWrapper<CangkuKucun> wrapper= Wrappers.<CangkuKucun>lambdaQuery();
        wrapper.eq(kuCunVo.getCangkuId()!=null,CangkuKucun::getCangkuId,kuCunVo.getCangkuId())
                .eq(kuCunVo.getGoodsId()!=null,CangkuKucun::getGoodsId,kuCunVo.getGoodsId());
        Page page=new Page(pageNo,pageSize);
        List<CangkuKucun> list=baseMapper.findAll(page,wrapper);
        return new PageResult(list,page.getTotal());
    }

    @Override
    public List<CangkuKucun> searchAll() {
        return cangkuKuCunMapper.selectList(null);
    }

    @Override
    public void add(CangkuKucun cangkuKucun) {
        cangkuKuCunMapper.insert(cangkuKucun);
    }

    @Override
    public void del(Integer id) {
cangkuKuCunMapper.deleteById(id);
    }

    @Override
    public void update(CangkuKucun cangkuKucun) {
cangkuKuCunMapper.updateById(cangkuKucun);
    }

    @Override
    public ResultVo zhuankuStore(CangkuKucun cangkuKuCun1,Integer userId, Integer cangkuId, Integer goodsId, Integer shuliang) {
        //查询库存
        Integer num = -1;
        Integer num1=-1;
        num1 = cangkuKuCun1.getShuliang().intValue()-shuliang.intValue();
        if (num1<0){
            return ResultVo.failure("转库数量不能大于当前数量");
        }
        cangkuKuCun1.setShuliang(num1);
        cangkuKuCunMapper.updateById(cangkuKuCun1);
        //添加库存变更操作记录
        KucunBiandong kucunBiandong1 = new KucunBiandong();
        kucunBiandong1.setUserid(userId);
        kucunBiandong1.setGoodsId(cangkuKuCun1.getGoodsId());
        kucunBiandong1.setNum(shuliang);
        kucunBiandong1.setOpertime(LocalDateTime.now());
        kucunBiandong1.setCangkuId(cangkuKuCun1.getCangkuId());
        kucunBiandong1.setType("2");
        kuCunBdMapper.insert(kucunBiandong1);

        CangkuKucun cangkuKucun = new CangkuKucun();
        cangkuKucun.setGoodsId(goodsId);
        if(cangkuId==null){
            return ResultVo.failure("请选择仓库");
        }else{
            cangkuKucun.setCangkuId(cangkuId);
        }
        List<CangkuKucun> cangkuKucunlList = cangkuKuCunMapper.getList(cangkuKucun,cangkuKucun.getGoodsId(),cangkuKucun.getCangkuId());
        //商品仓库库存数量
        //如果存在库存，检查库存是否足够
        if(null != cangkuKucunlList && cangkuKucunlList.size() > 0){
            cangkuKucun = cangkuKucunlList.get(0);
            num = cangkuKucun.getShuliang().intValue()+shuliang.intValue();
            //库存充足，则更新库存数量
            cangkuKucun.setShuliang(num);
            cangkuKuCunMapper.updateById(cangkuKucun);

            //添加库存变更操作记录
            KucunBiandong kucunBiandong = new KucunBiandong();
            kucunBiandong.setUserid(userId);
            kucunBiandong.setGoodsId(goodsId);
            kucunBiandong.setNum(shuliang);
            kucunBiandong.setOpertime(LocalDateTime.now());
            kucunBiandong.setCangkuId(cangkuId);
            kucunBiandong.setType("1");
            kuCunBdMapper.insert(kucunBiandong);

        }else {
            num = 0 + shuliang.intValue();
            cangkuKucun.setShuliang(num);
            cangkuKucun.setGoodsId(goodsId);
            cangkuKucun.setCangkuId(cangkuId);
            cangkuKuCunMapper.insert(cangkuKucun);

            //添加库存变更操作记录
            KucunBiandong kucunBiandong = new KucunBiandong();
            kucunBiandong.setUserid(userId);
            kucunBiandong.setGoodsId(goodsId);
            kucunBiandong.setNum(num);
            kucunBiandong.setOpertime(LocalDateTime.now());
            kucunBiandong.setCangkuId(cangkuId);
            kucunBiandong.setType("1");
            kuCunBdMapper.insert(kucunBiandong);


        }
            return   ResultVo.success("转库成功");
    }
}
